package wili.b1.transform;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import wili.attrMapping._user.b1.AttConst;
import wili.attrMapping.file.JsonFile;
import wili.b1.transform.Conf.FILE;
import wili.c.Ca;
import wili.c.File9a;
import wili.c.Misc9a;
import wili.json.JsonLeaf;
import wili.json.JsonNode;
import wili.json.JsonObj;

/**
 *
 */
class JsonTransform4b1_HBox implements AttConst {
	String EDITABLE = "editable", VISIBLE = "visible", FORMULA = "formula", UPDATABLE = "updatable";
	JsonFile jsonFile;

	final JsonObj root;

	File toFile;

	public static void main(String[] args) {
		File from = FILE.to1, to = FILE.to2;
		JsonTransform4b1_HBox obj = new JsonTransform4b1_HBox(from, to);
		String ret = obj.transform();
		File9a.str2file(ret, to, -1);
		Ca.log(1, "\nSave to file: " + to.getName());
	}

	public JsonTransform4b1_HBox(File fromFile, File toFile) {
		this.jsonFile = new JsonFile(fromFile);
		this.root = this.jsonFile.root;
		this.toFile = toFile;
	}

	public JsonTransform4b1_HBox(String fromSt) {
		this.jsonFile = new JsonFile(fromSt);
		this.root = this.jsonFile.root;
	}

	public String transform() {
		twoItems_to_HBox(1);
		String retSt = jsonFile.refreshJsonSt();
		return retSt;
	}

	void twoItems_to_HBox(int use) {
		if (use < 1)
			return;
		List<JsonNode> itmesList = root.findNodesByName(true, "items");
		for (JsonNode items : itmesList) {
			JsonNode dad = items.dad;
			if (dad.valueOfSon(ctrlType, "").equals("HBox"))
				continue;
			if (items.sons().size() == 2) {
				JsonObj son = items.son(0), son2 = items.son(1);
				boolean is = false;
				Set<String> eds = new HashSet<>(), vis = new HashSet<>();
				for (JsonNode ton : items.sons()) {
					if (ton.son(ctrlType, "CheckBox") != null) {
						is = true;
					}
					eds.add(ton.valueOfSon(EDITABLE, ""));
					vis.add(ton.valueOfSon(VISIBLE, ""));
				}
				if (!is)
					continue;
				for (JsonNode ton : items.sons()) {
					ton.removeSon(EDITABLE, null);
					ton.removeSon(VISIBLE, null);
				}
				Misc9a.removePredi(eds, (va) -> va == null);
				Misc9a.removePredi(vis, (va) -> va == null);
				dad.addSon(new JsonLeaf(ctrlType, "HBOX"));
				{
					JsonNode att2 = dad.son(EDITABLE);
					if (!(att2 instanceof JsonLeaf))
						continue;
					JsonLeaf att = att2.toLeaf();
					if (att == null) {
						dad.addSon(att = new JsonLeaf(EDITABLE, ""));
					}
					for (String ed : eds) {
						if (!att.stringValue.isEmpty())
							att.stringValue += " && ";
						att.stringValue += ed;
					}
				}
				{
					JsonLeaf att = dad.son(VISIBLE);
					if (att == null) {
						dad.addSon(att = new JsonLeaf(VISIBLE, ""));
					}
					for (String ed : vis) {
						if (!att.stringValue.isEmpty())
							att.stringValue += " && ";
						att.stringValue += ed;
					}
				}
				if (dad.son(TEXT) != null) {
					if (dad.son(LABEL) == null) {
						dad.son(TEXT).renameTo(LABEL);
					} else {
						Ca.asert(false, "Has text and label son:" + dad.toString());
					}
				}
			}
		}
	}
}
